<%@ page import="entity.User" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.Map" %>
<%@ page import="java.util.HashMap" %><%--
  Created by IntelliJ IDEA.
  User: teacher
  Date: 2022/12/14
  Time: 14:57
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>EL表达式</title>
</head>
<body>
<%
    // 模拟存储数据
    // 此处代码当做不在JSP中
    pageContext.setAttribute("msg","Hello pageContext");
    request.setAttribute("msg","Hello request");
    session.setAttribute("msg","Hello session");
    application.setAttribute("msg","Hello application");

    // 存储对象
    User user = new User(1,"admin","123456","13812345678","江苏-南京");
    User user2 = new User(2,"jack","123456","13812345678","江苏-南京");
    request.setAttribute("user",user);
    List<User> users = new ArrayList<User>();
    users.add(user);
    users.add(user2);
    request.setAttribute("users",users);

    Map<String,User> map = new HashMap<String,User>();
    map.put("u1",user);
    map.put("u2",user2);
    request.setAttribute("map",map);

    request.setAttribute("age",65);
%>

<h1>访问作用域数据</h1>
<!--
    使用EL表达式的作用域对象.key表示获取指定作用域中指定key的数据
    相当于xxx.getAttribute("key")
-->
pageContext:${pageScope.msg}<br>
request:${requestScope.msg}<br>
session:${sessionScope.msg}<br>
application:${applicationScope.msg}<br>
<!--
    表达式内容直接写key,表示自动在四个作用域中进行搜索
    搜索顺序由小到大
    pageContext->request->session->application
    只要在某一个作用域中获取到了指定数据,则直接返回,不再向下搜索
    如果四个作用域中均没有数据,则直接什么都不返回
-->
msg:${msg}<br>
msg:${aaa}<br>
<h1>访问应用名</h1>
应用名:<%
    // request.getContextPath();
    HttpServletRequest req = (HttpServletRequest) pageContext.getRequest();
    String path = req.getContextPath();
    out.print(path);
%><br>
<!--
    在EL表达式中,遇到对象.getXxx()时直接使用对象.xxx即可
    对象.属性相当于get/set方法
-->
应用名:${pageContext.request.contextPath}
<h1>访问对象</h1>
<!--
    对于get方法,直接使用对象.属性名
-->
${user}--${user.id}--${user.username}--${user.password}--${user.phone}--${user.address}
<h2>访问List集合</h2>
${users[0].id}--${users[0].username}<br>
${users[1].id}--${users[1].username}<br>
<h2>访问Map集合</h2>
${map.u1.id}--${map.u1.username}<br>
${map['u2'].id}--${map['u2'].username}<br>
<h1>运算</h1>
${3+2}<br>
${3-2}<br>
${3*2}<br>
${3/2}<br>
${3%2}<br>
${"1" eq "1"}<br>
${"1" == "1"}<br>
${age >= 18 ? "成年了" : "未成年"}<br>
${empty user}<br>
${empty emp}<br>
${age >= 18 and age <= 60}<br>
${age >= 18 or age <= 60}<br>

</body>
</html>
